package com.jt.www.remote;

import com.jt.www.domain.dto.JtBranch;
import com.jt.www.domain.dto.OfflineRegisterDTO;
import com.jt.www.domain.vo.*;
import com.jt.www.model.mail.vo.JtBranchAccountVo;
import com.jt.www.model.registerCustomer.vo.JtBranchInfoVo;
import com.jt.www.model.registerCustomer.vo.UserManagerResultVo;
import com.jt.www.model.remote.user.EduAuthenInfoDetails;
import com.jt.www.model.remote.user.LoginResultVO;
import com.jt.www.model.remote.user.UserListDownloadVo;
import com.jt.www.model.reps.GenericDataResponse;
import com.jt.www.model.reps.GenericListResponse;
import com.jt.www.model.reps.GenericResponse;
import com.jt.www.model.vo.EduOrgListVO;
import com.jt.www.model.vo.EduOrgsVO;
import com.jt.www.model.vo.woxuebao.PersonalUserAuthenParam;
import config.FeignConfiguration;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@FeignClient(value = "user-svc", url = "${remote.user_svc_url}",
        configuration = FeignConfiguration.class,
        fallbackFactory = UserClient.FeignClientFallbackFactory.class)
public interface UserClient {

    public static final Logger log = LoggerFactory.getLogger(UserClient.class);

    /**
     * 通过用户编码与系统编码查询用户信息
     *
     * @param userCode
     * @param sysCode
     * @return
     */
    @GetMapping("/api/v1/queryUserInfo")
    ResponseEntity<GenericDataResponse<LoginResultVO>> queryInfoByUserCode(@RequestParam("userCode") String userCode, @RequestParam("sysCode") String sysCode);

    /**
     * 查询教育 学校/企业认证信息
     *
     * @param userCode
     * @return
     */
    @GetMapping("/api/v1/queryAuthentEduInfo")
    ResponseEntity<GenericDataResponse<EduAuthenInfoDetails>> queryAuthentEduInfo(@RequestParam("userCode") String userCode);

    /**
     * 查询渠道个人认证信息服务接口
     *
     * @param param
     * @param channelCode
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/api/v1/queryPersonalUserAuthenInfo", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    ResponseEntity<GenericDataResponse<Map<String, Object>>> queryPersonalUserAuthenInfo(@RequestParam("param") String param,
                                                                                         @RequestParam("channelCode") String channelCode) throws Exception;

    /**
     * 渠道个人认证服务接口
     *
     * @param personalAuthenParam
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/api/v1/personalUserAuthen",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseEntity<GenericDataResponse<Map<String, Object>>> personalUserAuthen(@RequestBody PersonalUserAuthenParam personalAuthenParam) throws Exception;

    /**
     * 根据省市区查询分支机构账户信息
     *
     * @param areaCode
     * @param channelNo
     * @param productCode
     * @return
     */
    @GetMapping(value = "/api/v1.5/user/branchAccount", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    ResponseEntity<GenericDataResponse<JtBranchAccountVo>> branchAccount(@RequestParam("areaCode") String areaCode,
                                                                         @RequestParam("channelNo") String channelNo,
                                                                         @RequestParam("productCode") String productCode);

    /**
     * 查看用户是否已经注册 0-未注册  1-已注册
     *
     * @param loginId
     * @return
     */
    @GetMapping(value = "/api/v1/queryUserIsExist", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    GenericDataResponse<Map<String, String>> queryUserIsExist(@RequestParam("loginId") String loginId);

    /**
     * 渠道后台注册用用户(用户中心提供的是之前给特设提供的接口)
     *
     * @param registerVO
     * @return
     */
    @PostMapping(value = "/api/v1/sequipRegister", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    GenericDataResponse<String> register(@RequestBody Map<String, String> registerVO);

    /**
     * @Description: 用户权限查询
     * @Param:
     * @return:
     * @Author: shalongteng
     * @Date: 2019-06-19
     */
    @GetMapping(value = "/api/v1/queryUserRoleAndAuthority", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}
            , consumes = "application/json;charset=UTF-8")
    ResponseEntity<GenericDataResponse<Map<String, Object>>> queryUserRoleAndAuthority(@RequestParam("userCode") String userCode,
                                                                                       @RequestParam("sysCode") String sysCode);

    /**
     * @Description: 用户信息查询
     * @Param:json
     * @return:
     * @Author: suntao
     * @Date: 2019-07-31
     */
    @PostMapping(value = "/api/eduUserList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseEntity<GenericListResponse<UserListDownloadVo>> queryUserInfoDownList(@RequestBody @Valid String json) throws Exception;

    /**
     * @Description: 机构信息查询
     * @Param:json
     * @return:
     * @Author: suntao
     * @Date: 2019-07-31
     */
    @PostMapping(value = "/api/eduOrgList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseEntity<GenericListResponse<EduOrgsVO>> queryOrgInfoList(@RequestBody EduOrgListVO eduOrgListVO) throws Exception;

    /**
     * 教保网学校列表
     *
     * @param map
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/api/eduOrgList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    GenericListResponse<Map<String, Object>> eduOrgList(@RequestBody Map<String, Object> map) throws Exception;

    /**
     * 查询分支机构下联系人员信息
     *
     * @param channelCode
     * @param jtBranchCode
     * @param linkName
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/api/v1/queryContactPerson", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    GenericListResponse<Map<String, Object>> queryContactPerson(@RequestParam("channelCode") String channelCode,
                                                                @RequestParam("jtBranchCode") String jtBranchCode,
                                                                @RequestParam("linkName") String linkName,
                                                                @RequestParam("pageNum") Integer pageNum,
                                                                @RequestParam("pageSize") Integer pageSize);

    /**
     * 根据角色编码，分支机构编码查询客户经理
     *
     * @param branchCode
     * @param roleCode
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/api/v1/selectChannelManager", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    GenericListResponse<UserManagerResultVo> selectChannelManager(@RequestParam("branchCode") String branchCode,
                                                                  @RequestParam("roleCode") String roleCode,
                                                                  @RequestParam("name") String name,
                                                                  @RequestParam("userLoginId") String userLoginId,
                                                                  @RequestParam("pageSize") Integer pageSize,
                                                                  @RequestParam("pageNum") Integer pageNum) throws Exception;

    /**
     * 查询私保全员营销，以及专营私保
     *
     * @param statrDate
     * @param endDate
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/api/v1/queryPvtUserInfo", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    GenericListResponse<UserManagerResultVo> queryPvtUserInfo(@RequestParam("statrDate") String statrDate,
                                                              @RequestParam("endDate") String endDate,
                                                              @RequestParam("userCode") String userCode) throws Exception;

    /**
     * 根据登陆账号查询用户角色信息（隐患排查业务员、风管所工程师）
     *
     * @param userLoginId
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/api/v1/eduQueryUserRole", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    GenericDataResponse<Map<String, Object>> eduQueryUserRole(@RequestParam("userLoginId") String userLoginId) throws Exception;

    /**
     * 用户中心登录
     *
     * @param registerAndLoginVO
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/api/v1/registerAndLogin", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    GenericDataResponse<LoginResultVO> registerAndLogin(@RequestBody RegisterAndLoginVO registerAndLoginVO) throws Exception;

    /**
     * 用户中心登录
     *
     * @param loginVO
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/api/v1/login", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    GenericDataResponse<LoginResultVO> login(@RequestBody LoginVO loginVO) throws Exception;

    /**
     * 查询分支机构下属三级机构
     *
     * @param jtBranchCode
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/api/v1/queryThirdGradeBranch", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    GenericListResponse<JtBranchInfoVo> queryThirdGradeBranch(@RequestParam("jrBranchCode") String jtBranchCode) throws Exception;

    /**
     * 查询分支机构上级机构
     *
     * @param jtBranchCode
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/api/v1/querySuperiorBranch", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    GenericDataResponse<JtBranchInfoVo> querySuperiorBranch(@RequestParam("jrBranchCode") String jtBranchCode) throws Exception;

    /**
     * 线下录入用户注册
     *
     * @param offlineRegisterDTOS
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/api/v1/medRecordOrgRegister", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    GenericListResponse<OfflineRegisterVo> medRecordOrgRegister(@RequestBody List<OfflineRegisterDTO> offlineRegisterDTOS) throws Exception;

    /**
     * 通过员工编号 查询用户信息
     *
     * @param employeeNum 员工编号
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/api/selectByEmployeeNum", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    GenericDataResponse<EmployeeVO> selectByEmployeeNum(@RequestParam("employeeNum") String employeeNum) throws Exception;

    /**
     * 渠道后台账号注册接口
     * 密码默认 jiangtai
     *
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/api/v1/registerByNameAndPhone", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    GenericListResponse<PhoneRegisterVO> registerByNameAndPhone(@RequestBody List<PhoneRegisterVO> registerVOS) throws Exception;

    /**
     * 账号状态修改
     *
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/api/loginLog", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    GenericResponse loginLog(@RequestParam("userCode") String userCode, @RequestParam("status") String status) throws Exception;

    /**
     * 根据渠道编码得到分支机构
     *
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/api/v1/getJtBranchByChannel", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    GenericListResponse<JtBranch> getJtBranchByChannel(@RequestParam("channelCode") String channelCode) throws Exception;

    @Component
    class FeignClientFallbackFactory implements FallbackFactory<UserClient> {

        @Override
        public UserClient create(Throwable cause) {
            return new UserClient() {

                @Override
                public ResponseEntity<GenericDataResponse<LoginResultVO>> queryInfoByUserCode(String userCode, String sysCode) {
                    log.error("{}-用户中心远程调用失败,fallback reason:", "queryInfoByCode", cause);
                    return null;
                }

                @Override
                public ResponseEntity<GenericDataResponse<EduAuthenInfoDetails>> queryAuthentEduInfo(String userCode) {
                    log.error("{}-用户中心远程调用失败,fallback reason:", "queryInfoByCode", cause);
                    return null;
                }

                @Override
                public ResponseEntity<GenericDataResponse<Map<String, Object>>> queryPersonalUserAuthenInfo(String channelCode, String param) {
                    log.error("调用查询渠道个人认证信息服务接口失败,fallback reason:", cause);
                    return null;
                }

                @Override
                public ResponseEntity<GenericDataResponse<Map<String, Object>>> personalUserAuthen(PersonalUserAuthenParam personalAuthenParam) throws Exception {
                    log.error("调用渠道个人认证服务接口失败,fallback reason:", cause);
                    return null;
                }

                @Override
                public ResponseEntity<GenericDataResponse<Map<String, Object>>> queryUserRoleAndAuthority(String userCode, String sysCode) {
                    log.error("调用用户中心用户权限查询接口失败,fallback reason:", cause);
                    return null;
                }

                @Override
                public ResponseEntity<GenericDataResponse<JtBranchAccountVo>> branchAccount(String areaCode, String channelNo, String productCode) {
                    log.error("调用用户中心，根据省市区查询分支机构账户信息失败,fallback reason:", cause);
                    return null;
                }

                @Override
                public GenericDataResponse<Map<String, String>> queryUserIsExist(String loginId) {
                    log.error("调用用户中心查看用户是否已经注册失败,fallback reason:", cause);
                    return null;
                }

                @Override
                public GenericDataResponse<String> register(Map<String, String> registerVO) {
                    log.error("调用用户中心，用户权限查询账户信息失败,fallback reason:", cause);
                    return null;
                }

                @Override
                public ResponseEntity<GenericListResponse<UserListDownloadVo>> queryUserInfoDownList(String json) throws Exception {
                    log.error("调用用户中心，渠道后台注册用用户(用户中心提供的是之前给特设提供的接口)失败,fallback reason:", cause);
                    return null;
                }

                @Override
                public ResponseEntity<GenericListResponse<EduOrgsVO>> queryOrgInfoList(EduOrgListVO eduOrgListVO) throws Exception {
                    log.error("调用用户中心查询机构列表失败,fallback reason:", cause);
                    return null;
                }

                @Override
                public GenericListResponse<Map<String, Object>> eduOrgList(Map<String, Object> map) throws Exception {
                    log.error("调用用户中心教保网学校列表接口失败,fallback reason:", cause);
                    return null;
                }

                @Override
                public GenericListResponse<Map<String, Object>> queryContactPerson(String channelCode, String jtBranchCode, String linkName, Integer pageNum, Integer pageSize) {
                    log.error("调用用户中心查询分支机构下联系人员信息接口失败,fallback reason:", cause);
                    return null;
                }

                @Override
                public GenericListResponse<UserManagerResultVo> selectChannelManager(String branchCode, String roleCode, String name, String userLoginId, Integer pageSize, Integer pageNum) throws Exception {
                    log.error("调用用户中心根据角色编码，分支机构编码查询客户经理接口失败,fallback reason:", cause);
                    return null;
                }

                @Override
                public GenericListResponse<UserManagerResultVo> queryPvtUserInfo(String startDate, String endDate, String userCode) throws Exception {
                    log.error("调用用户中心查询私保全员营销，以及专营私保接口失败,fallback reason:", cause);
                    return null;
                }

                @Override
                public GenericDataResponse<Map<String, Object>> eduQueryUserRole(String userLoginId) throws Exception {
                    log.error("调用用户中心根据登陆账号查询用户角色信息（隐患排查业务员、风管所工程师）接口失败,fallback reason:", cause);
                    return null;
                }

                @Override
                public GenericDataResponse<LoginResultVO> registerAndLogin(RegisterAndLoginVO registerAndLoginVO) throws Exception {
                    log.error("调用用户中心登录与注册接口失败,fallback reason:", cause);
                    return null;
                }

                @Override
                public GenericDataResponse<LoginResultVO> login(LoginVO loginVO) throws Exception {
                    log.error("调用用户中心登录接口失败,fallback reason:", cause);
                    return null;
                }

                @Override
                public GenericListResponse<JtBranchInfoVo> queryThirdGradeBranch(String jtBranchCode) throws Exception {
                    log.error("调用用户中心查询分支机构下属三级机构接口失败,fallback reason:", cause);
                    return null;
                }

                @Override
                public GenericDataResponse<JtBranchInfoVo> querySuperiorBranch(String jtBranchCode) throws Exception {
                    log.error("调用用户中心查询分支机构上级机构接口失败,fallback reason:", cause);
                    return null;
                }

                @Override
                public GenericListResponse<OfflineRegisterVo> medRecordOrgRegister(List<OfflineRegisterDTO> offlineRegisterDTOS) throws Exception {
                    log.error("调用用户中心线下企业注册接口失败,fallback reason:", cause);
                    return null;
                }

                @Override
                public GenericDataResponse<EmployeeVO> selectByEmployeeNum(String employeeNum) throws Exception {
                    log.error("通过员工编码查询员工信息失败,fallback reason:", cause);
                    return null;
                }

                @Override
                public GenericListResponse<PhoneRegisterVO> registerByNameAndPhone(List<PhoneRegisterVO> phoneRegisterVOs) throws Exception {
                    log.error("行管调用用户中心注册失败,fallback reason:", cause);
                    return null;
                }

                @Override
                public GenericResponse loginLog(String userCode, String status) throws Exception {
                    log.error("行管调用用户中心修改状态失败,fallback reason:", cause);
                    return null;
                }

                @Override
                public GenericListResponse<JtBranch> getJtBranchByChannel(String channelCode) throws Exception {
                    log.error("根据渠道编码得到分支机构失败,fallback reason:", cause);
                    return null;
                }

            };
        }
    }


}